package com.w.tank;
import java.awt.*;
import java.util.Random;


public class Tank extends GameObject{
    private Dir dir = Dir.DOWN;
    static final int WIDTH = ResourceMgr.tankD.getWidth(), HEIGHT= ResourceMgr.tankD.getHeight();
    static final int speed = 5;
    boolean moving = false;
    private Group group = Group.BAD;
    private Random random = new Random();
    int old_x,old_y;
    FireStrage fs = new DefaultFireStrage();

    public void setMoving(boolean moving) {
        this.moving = moving;
    }
    Rectangle rect = new Rectangle();

    public void setX(int x) {
        this.x = x;
    }

    public Group getGroup() {
        return group;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }
    @Override
    public int getWidth() {
        return WIDTH;
    }

    @Override
    public int getHeight() {
        return HEIGHT;
    }

    public Tank(int x, int y, Dir dir, Group group){
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group = group;
        if(group == Group.BAD){
            moving = true;
        }
        rect.width = WIDTH;
        rect.height = HEIGHT;
        GameControl.getInstance().add(this);
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Dir getDir() {
        return dir;
    }

    public static int getSpeed() {
        return speed;
    }

    @Override
    public String toString() {
        return "Tank { " + group + " }";
    }

    public void paint(Graphics g){
        if(!isLive()){
            GameControl.getInstance().remove(this);
        }

        switch (dir){
            case RIGHT:
                g.drawImage(ResourceMgr.tankR, x, y, null);
                break;
            case LEFT:
                g.drawImage(ResourceMgr.tankL, x, y, null);
                break;
            case DOWN:
                g.drawImage(ResourceMgr.tankD, x, y, null);
                break;
            case UP:
                g.drawImage(ResourceMgr.tankU, x, y, null);
                break;
            default:
                break;

        }
        move();
    }

    private void move() {
        old_x = x;
        old_y = y;

        if (moving) {
            switch (dir) {
                case UP:
                    if(y - HEIGHT/2 -speed>=0)
                        y -= speed;
                    break;
                case DOWN:
                    if(y+speed+HEIGHT <= TankFrame.GAMT_HEIGHT)
                        y += speed;
                    break;
                case LEFT:
                    if(x - speed >=0)
                        x -= speed;
                    break;
                case RIGHT:
                    if(x + WIDTH + speed <= TankFrame.GAME_WIDTH)
                        x += speed;
                    break;
                default:
                    break;
            }
        }
            if(random.nextInt(7) >= 5 && group==Group.BAD)
                fs.fire(this);
             if(group == Group.BAD && random.nextInt(100) > 95)
                 dir = Dir.values()[random.nextInt(4)];
        rect.x = x;
        rect.y = y;




    }


}
